package org.xtense.rcp.views;

import org.xtense.rcp.util.ITreeEntry;

import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;

public class TreeViewProvider extends LabelProvider implements
		ITreeContentProvider {

	@Override
	public Object[] getChildren(Object parentElement) {
		ITreeEntry entry = (ITreeEntry) parentElement;
		return entry.getChildren().toArray();
	}

	@Override
	public Object getParent(Object element) {
		return ((ITreeEntry) element).getParentEntry();
	}

	@Override
	public boolean hasChildren(Object element) {
		ITreeEntry entry = (ITreeEntry) element;
		return entry.getChildren().isEmpty();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object[] getElements(Object inputElement) {
		if (inputElement instanceof List) {
			return ((List) inputElement).toArray();
		} else {
			return new Object[0];
		}
	}

	@Override
	public Image getImage(Object element) {
		return ((NavigatorEntry) element).getImage();
	}

	@Override
	public String getText(Object element) {
		return ((NavigatorEntry) element).getName();
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		//do nothing
	}

}
